package com.apac.controllers;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jmesa.limit.ExportType;
import org.jmesa.model.TableModel;
import org.jmesa.view.component.Column;
import org.jmesa.view.component.Row;
import org.jmesa.view.component.Table;
import org.jmesa.view.html.component.HtmlColumn;
import org.jmesa.view.html.component.HtmlRow;
import org.jmesa.view.html.component.HtmlTable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.apac.bom.TestObject;
import com.apac.dao.TestDao;

@Controller
@RequestMapping("/clearances")
public class ClearanceController {

	@RequestMapping(value="", method=RequestMethod.GET)
	public ModelAndView clearance(HttpServletRequest request, HttpServletResponse response) {

		ModelAndView mv = new ModelAndView("Clearance");
		
		String clearances = generateClearances(request, response);
		String summary = generateSummary(request, response);
		
		mv.addObject("clearances",clearances);
		mv.addObject("summary",summary);
		return mv;
	}
	
	public String generateSummary(HttpServletRequest request, HttpServletResponse response){

		/**MOCK DATA**/
		ArrayList<TestObject> data = new ArrayList<TestObject>();
		TestDao tDao = new TestDao();
		data = tDao.retrieveAll();
		/**MOCK DATA**/
		
		TableModel tableModel = new TableModel("summary", request, response);
		
		tableModel.setExportTypes(ExportType.CSV,ExportType.JEXCEL,ExportType.PDFP);
		tableModel.setItems(data);
		
		if(tableModel.isExporting()){
			Table Table = new Table().caption("Clearance Record");
			
			Row Row = new Row();
			Table.setRow(Row);
			
			Column col1 = new Column("att2").title("Date");
			Row.addColumn(col1);
	
			Column col2 = new Column("att1").title("ADB");
			Row.addColumn(col2);
	
			Column col3 = new Column("att2").title("DB");
			Row.addColumn(col3);
	
			Column col4 = new Column("att3").title("Globe");
			Row.addColumn(col4); 
	
			Column col5 = new Column("att1").title("HP");
			Row.addColumn(col5);
	
			Column col6 = new Column("att2").title("IBM");
			Row.addColumn(col6);
	
			Column col7 = new Column("att3").title("UHG");
			Row.addColumn(col7); 
	
			Column col8 = new Column("att1").title("(Blank)");
			Row.addColumn(col8);
	
			Column col9 = new Column("att2").title("Total");
			Row.addColumn(col9);
	
			tableModel.setTable(Table);
			
			String export = tableModel.render();
			return export;
		}
		else{
			HtmlTable htmlTable = new HtmlTable();
	
			HtmlRow HtmlRow = new HtmlRow();
			htmlTable.setRow(HtmlRow);
						
			HtmlColumn col1 = new HtmlColumn("att2").title("Date");
			HtmlRow.addColumn(col1);
	
			HtmlColumn col2 = new HtmlColumn("att1").title("ADB");
			HtmlRow.addColumn(col2);
	
			HtmlColumn col3 = new HtmlColumn("att2").title("DB");
			HtmlRow.addColumn(col3);
	
			HtmlColumn col4 = new HtmlColumn("att3").title("Globe");
			HtmlRow.addColumn(col4); 
	
			HtmlColumn col5 = new HtmlColumn("att1").title("HP");
			HtmlRow.addColumn(col5);
	
			HtmlColumn col6 = new HtmlColumn("att2").title("IBM");
			HtmlRow.addColumn(col6);
	
			HtmlColumn col7 = new HtmlColumn("att3").title("UHG");
			HtmlRow.addColumn(col7); 
	
			HtmlColumn col8 = new HtmlColumn("att1").title("(Blank)");
			HtmlRow.addColumn(col8);
	
			HtmlColumn col9 = new HtmlColumn("att2").title("Total");
			HtmlRow.addColumn(col9);
	    	
//	    	HtmlColumn viewLink = new HtmlColumn("id").title("View");
//	    	viewLink.setCellEditor(new CellEditor() {             
//				public Object getValue(Object item, String property, int rowcount) {                 
//	    			//Object value = new HtmlCellEditor().getValue(item, property, rowcount);                 
//	    			HtmlBuilder html = new HtmlBuilder();
//	    			ExamResponse er = (ExamResponse)item;
//	    			html.a().href().quote().append("JMesaExportStudentExamServlet?id="+er.getExamResponseID()).quote().close();
//	    			html.append("View Response");
//	    			html.aEnd();
//	    			return html.toString();             
//	    			}         
//	    		});
//	    	viewLink.setSortable(false);
//	    	viewLink.setFilterable(false);
//	    	htmlRow.addColumn(viewLink); 
	
			tableModel.setTable(htmlTable);
	
			String html = tableModel.render();
			return html;
		}
	}
	
	public String generateClearances(HttpServletRequest request, HttpServletResponse response){

		/**MOCK DATA**/
		ArrayList<TestObject> data = new ArrayList<TestObject>();
		TestDao tDao = new TestDao();
		data = tDao.retrieveAll();
		/**MOCK DATA**/
		
		TableModel tableModel = new TableModel("clearances", request, response);
		
		tableModel.setExportTypes(ExportType.CSV,ExportType.JEXCEL,ExportType.PDFP);
		tableModel.setItems(data);
		
		if(tableModel.isExporting()){
			Table Table = new Table().caption("TestObject Record");
			
			Row Row = new Row();
			Table.setRow(Row);
			
			Column col1 = new Column("id").title("No.");
			Row.addColumn(col1);
	
			Column col2 = new Column("att1").title("Name");
			Row.addColumn(col2);
	
			Column col3 = new Column("att2").title("Account");
			Row.addColumn(col3);
	
			Column col4 = new Column("att3").title("Clearance Management Notification");
			Row.addColumn(col4); 
	
			Column col5 = new Column("att1").title("Last Day");
			Row.addColumn(col5);
	
			Column col6 = new Column("att2").title("Hold Payroll Date");
			Row.addColumn(col6);
	
			Column col7 = new Column("att3").title("Period of Last Pay");
			Row.addColumn(col7); 
	
			Column col8 = new Column("att1").title("Clearance Received");
			Row.addColumn(col8);
	
			Column col9 = new Column("att2").title("With Liability");
			Row.addColumn(col9);
	
			Column col10 = new Column("att2").title("Liability");
			Row.addColumn(col10);
	
			Column col11 = new Column("att2").title("Clearance Submitted to HR");
			Row.addColumn(col11);
	
			Column col12 = new Column("att3").title("Target Check Release Date");
			Row.addColumn(col12); 
	
			Column col13 = new Column("att1").title("Last Pay For Audit");
			Row.addColumn(col13);
	
			Column col14 = new Column("att2").title("Check Availability ");
			Row.addColumn(col14);
	
			Column col15 = new Column("att2").title("Released Date");
			Row.addColumn(col15);
	
			Column col16 = new Column("att2").title("Remarks");
			Row.addColumn(col16);
	
			tableModel.setTable(Table);
			
			String export = tableModel.render();
			return export;
		}
		else{
			HtmlTable htmlTable = new HtmlTable();
	
			HtmlRow HtmlRow = new HtmlRow();
			htmlTable.setRow(HtmlRow);
						
			HtmlColumn col1 = new HtmlColumn("id").title("No.");
			HtmlRow.addColumn(col1);
	
			HtmlColumn col2 = new HtmlColumn("att1").title("Name");
			HtmlRow.addColumn(col2);
	
			HtmlColumn col3 = new HtmlColumn("att2").title("Account");
			HtmlRow.addColumn(col3);
	
			HtmlColumn col4 = new HtmlColumn("att3").title("Clearance Management Notification");
			HtmlRow.addColumn(col4); 
	
			HtmlColumn col5 = new HtmlColumn("att1").title("Last Day");
			HtmlRow.addColumn(col5);
	
			HtmlColumn col6 = new HtmlColumn("att2").title("Hold Payroll Date");
			HtmlRow.addColumn(col6);
	
			HtmlColumn col7 = new HtmlColumn("att3").title("Period of Last Pay");
			HtmlRow.addColumn(col7); 
	
			HtmlColumn col8 = new HtmlColumn("att1").title("Clearance Received");
			HtmlRow.addColumn(col8);
	
			HtmlColumn col9 = new HtmlColumn("att2").title("With Liability");
			HtmlRow.addColumn(col9);
	
			HtmlColumn col10 = new HtmlColumn("att2").title("Liability");
			HtmlRow.addColumn(col10);
	
			HtmlColumn col11 = new HtmlColumn("att2").title("Clearance Submitted to HR");
			HtmlRow.addColumn(col11);
	
			HtmlColumn col12 = new HtmlColumn("att3").title("Target Check Release Date");
			HtmlRow.addColumn(col12); 
	
			HtmlColumn col13 = new HtmlColumn("att1").title("Last Pay For Audit");
			HtmlRow.addColumn(col13);
	
			HtmlColumn col14 = new HtmlColumn("att2").title("Check Availability ");
			HtmlRow.addColumn(col14);
	
			HtmlColumn col15 = new HtmlColumn("att2").title("Released Date");
			HtmlRow.addColumn(col15);
	
			HtmlColumn col16 = new HtmlColumn("att2").title("Remarks");
			HtmlRow.addColumn(col16);
	    	
//	    	HtmlColumn viewLink = new HtmlColumn("id").title("View");
//	    	viewLink.setCellEditor(new CellEditor() {             
//				public Object getValue(Object item, String property, int rowcount) {                 
//	    			//Object value = new HtmlCellEditor().getValue(item, property, rowcount);                 
//	    			HtmlBuilder html = new HtmlBuilder();
//	    			ExamResponse er = (ExamResponse)item;
//	    			html.a().href().quote().append("JMesaExportStudentExamServlet?id="+er.getExamResponseID()).quote().close();
//	    			html.append("View Response");
//	    			html.aEnd();
//	    			return html.toString();             
//	    			}         
//	    		});
//	    	viewLink.setSortable(false);
//	    	viewLink.setFilterable(false);
//	    	htmlRow.addColumn(viewLink); 
	
			tableModel.setTable(htmlTable);
	
			String html = tableModel.render();
			return html;
		}
	}
}
